package com.example.permission.controller;

import com.example.permission.entity.Role;
import com.example.permission.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    // 任务二：跳转到角色菜单查询页面
    @GetMapping("/role/menu")
    public String toRoleMenuPage() {
        return "role-menu"; // 对应templates/role-menu.html
    }

    // 任务二：处理角色菜单查询请求（支持ID或名称查询）
    @PostMapping("/role/queryWithMenus")
    public String queryRoleWithMenus(
            @RequestParam(required = false) Long roleId, // 可选参数
            @RequestParam(required = false) String roleKey, // 可选参数
            Model model) {

        Role role = null;
        // 根据传入的参数查询
        if (roleId != null) {
            role = roleService.getRoleWithMenusById(roleId);
        } else if (roleKey != null && !roleKey.isEmpty()) {
            role = roleService.getRoleWithMenusByKey(roleKey);
        }

        // 结果存入model
        model.addAttribute("role", role);
        return "role-menu"; // 回到查询页面展示结果
    }
}